// import axios from 'axios'
import {Message} from 'element-ui'
import router from 'router'
import store from 'store'

/**
 * 网络请求封装
 * @param {Object} config 
 */
export function request(config){
    const instance = axios.create({
        baseURL: 'http://140.143.142.156:8088',
        timeout: 5000
    })

    // 前置拦截
    instance.interceptors.request.use(config => {
        return config
    })
  
    //后置拦截
    instance.interceptors.response.use(response => {
      let res = response.data;
  
      if (res.code === 200) {
        return response
      } else {
  
        Message.error('错了哦，这是一条错误消息', {duration: 3 * 1000})
  
        return Promise.reject(response.data.msg)
      }
    },
    error => {
      console.log(error)
      if(error.response.data) {
        error.message = error.response.data.msg
      }
  
      if(error.response.status === 401) {
        store.commit("REMOVE_INFO")
        router.push("/login")
      }
  
      Message.error(error.message, {duration: 3 * 1000})
      return Promise.reject(error)
    })

    return instance(config)
}